import axios from 'axios';
import {getToken} from '@/utils/auth'

//创建文件下载的axios实例
var token = 'Bearer ' + getToken();
const downloadService = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: process.env.VUE_APP_BASE_API,
    // 超时
    timeout: 10000,
    headers: {'Authorization': token}
})


export function download(url,data) {
    var newData = {
        docName: data.docName,
        docUrl: data.docUrl
    }
    return downloadService({
        url: url,
        method: 'get',
        params: newData,
        responseType: 'blob'//接收类型设置，否者返回字符型
    }).then(res => {           //定义文件名等相关信息
        const blob = res.data
        const reader = new FileReader();
        reader.readAsDataURL(blob)
        reader.onload = (e) => {
            const a = document.createElement('a');
            a.download = data.docName;
            a.href = e.target.result;
            document.body.appendChild(a);
            a.click()
            document.body.removeChild(a);
        }
    })
}
